% Display app res, phase & asso errors
% with impedance tensor & erros as input
% tested 9/11/02 -ok
% changed the error bar plotting 12/11/02
% known bug - if (Rhxy - RhxyErr/2) <0, log plot will propmt an error message
% latest date 13.10.2002

function[Rhxy,Rhyx,Phxy,Phyx] = dispresph(tf,ProcDef),

frq=ProcDef.TLFreq1;
file=ProcDef.file1;
nfrq = length(frq);

for i = 1:nfrq,   
   Phxy(i) = atan2(imag(tf(i,1)),real(tf(i,1))) * 180 / 3.1415926535897;
   Rhxy(i) = 0.2 * (1/frq(i)) * (tf(i,1) * conj(tf(i,1)));
   Phyx(i) = (atan2(imag(tf(i,2)),real(tf(i,2))) * 180 / 3.1415926535897);
   Rhyx(i) = 0.2 * (1/frq(i)) * (tf(i,2) * conj(tf(i,2)));
end;

k=find(Phyx>-180&Phyx<-90);
Phyx(k)=Phyx(k)+180;
k=find(Phxy<-180&Phxy<-90);
Phxy(k)=Phxy(k)+180;

k=find(Phyx>-90&Phyx<-0);
Phyx(k)=Phyx(k)+90;
k=find(Phxy>-90&Phxy<-0);
Phxy(k)=Phxy(k)+90;


PhxyErr = (tf(:,5)./abs(tf(:,1)))*(180/pi); 
PhyxErr = (tf(:,6)./abs(tf(:,2)))*(180/pi);
RhxyErr = (tf(:,5).*abs(tf(:,1))./frq') * 0.4; % bit confusion. may be ok -dt 2.8.2
RhyxErr = (tf(:,6).*abs(tf(:,2))./frq') * 0.4;

%--------------------------------------------------------------
T = 1./frq;

mnx=ceil(log10(max(T)))+1;
mux=floor(log10(min(T)))-1;
mny=ceil(log10(max([Rhxy Rhyx])))+1;
muy=floor(log10(min([Rhyx Rhxy])))-1;

c1 = 'r*';
c2 = 'b*';
c3 = 'red';
c4 = 'blue';


subplot(2,1,1);
%plot(log10(T'),log10(Rhxy),'r.',log10(T),log10(Rhyx),'b.',log10(T'),log10(Rhxy),'r-',log10(T),log10(Rhyx),'b-');

if ishold == 1,
   c1 = 'k*';
   c2 = 'g*';
   c3 = 'k';
   c4 = 'g';
end;

   plot(log10(T'),log10(Rhxy),c1,log10(T),log10(Rhyx),c2);
for i = 1:nfrq,
   tt = log10(Rhxy(i));
   kk = log10(Rhxy(i)+RhxyErr(i)/2);
   vv = 2*tt-kk;
   h=line(log10([T(i) T(i)]),[kk vv]);
   set(h,'color',c3);
   tt = log10(Rhyx(i));
   kk = log10(Rhyx(i)+RhyxErr(i)/2);
   vv = 2*tt-kk;

  	h=line(log10([T(i) T(i)]),[kk vv]);
   set(h,'color',c4);

end;
axis([mux mnx muy mny]);
grid;
ylabel('log ohm-m');
xlabel('log period (s)');
legend('XY','YX');
hold on;
subplot(2,1,2);
plot(log10(T),Phxy,c1,log10(T),Phyx,c2);

for i = 1:nfrq,
	h=line(log10([T(i) T(i)]),[Phxy(i)+PhxyErr(i)/2,Phxy(i)-PhxyErr(i)/2]);
   set(h,'color',c3);
   h=line(log10([T(i) T(i)]),[Phyx(i)+PhyxErr(i)/2,Phyx(i)-PhyxErr(i)/2]);
   set(h,'color',c4);
end;

axis([mux mnx 0 90]);
grid;

xlabel('log period (s)');
ylabel('degrees');
legend('XY','YX');
title(file);
hold on;
